import 'package:azlistview/azlistview.dart';
import 'package:flutter/material.dart';
import 'package:flutter_deer/account/iview/bank_iview.dart';
import 'package:flutter_deer/account/models/bank_entity.dart';
import 'package:flutter_deer/account/presenter/bank_presenter.dart';
import 'package:flutter_deer/account/provider/bank_provider.dart';
import 'package:flutter_deer/mvp/base_page.dart';
import 'package:flutter_deer/mvp/power_presenter.dart';
import 'package:flutter_deer/res/constant.dart';
import 'package:flutter_deer/routers/fluro_navigator.dart';
import 'package:flutter_deer/util/image_utils.dart';
import 'package:flutter_deer/util/other_utils.dart';
import 'package:flutter_deer/widgets/my_app_bar.dart';
import 'package:flutter_deer/widgets/state_layout.dart';
import 'package:flutter_gen/gen_l10n/deer_localizations.dart';
import 'package:provider/provider.dart';

class BankSelectPage extends StatefulWidget {
  
  const BankSelectPage({super.key, this.type = 0});
  
  final int type;
  
  @override
  _BankSelectPageState createState() => _BankSelectPageState();
}

class _BankSelectPageState extends State<BankSelectPage> with BasePageMixin<BankSelectPage, PowerPresenter>, AutomaticKeepAliveClientMixin<BankSelectPage> implements BankIMvpView {

  BankProvider provider = BankProvider();

  @override
  bool get wantKeepAlive => true;
  late BankPagePresenter bankPagePresenter;

  @override
  PowerPresenter<dynamic> createPresenter() {
    final PowerPresenter<dynamic> powerPresenter = PowerPresenter<dynamic>(this);
    bankPagePresenter = new BankPagePresenter();
    powerPresenter.requestPresenter([bankPagePresenter]);
    return powerPresenter;
  }

  List<String> _indexBarData = <String>[];

  @override
  void setCommonUserBankList(List<BankEntity>? bankList){
    provider.setCommonUseBankList(bankList);
  }

  @override
  void setBankList(List<BankEntity>? bankList){
    provider.setBankList(bankList);
  }
  
  @override
  void initState() {
    super.initState();
    bankPagePresenter.fetchList();
  }

  @override
  void dispose() {
    provider.dispose();
    super.dispose();
  }
  
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider<BankProvider>(
      create: (_) => provider,
      child: Scaffold(
        appBar: MyAppBar(
          title: widget.type == 0 ? '${DeerLocalizations.of(context)!.openingBank.nullSafe}' : '${DeerLocalizations.of(context)!.chooseBank.nullSafe}',
        ),
        body: SafeArea(
          child: Consumer<BankProvider>(
            builder: (_,provider,child){
              if(provider.commonUseBankList==StateType.loading||provider.bankListStateType==StateType.loading){
                return StateLayout(type: StateType.loading);
              }
              return AzListView(
                data: provider?.bankList??[],
                itemCount: provider?.bankList?.length??0,
                itemBuilder: (_, int index) {
                  if (index == 0 && widget.type == 0) {
                    return _buildHeader(provider);
                  }
                  BankEntity bankEntity = provider?.commonUseBankList?.elementAt(index)??BankEntity();
                  return _buildListItem(bankEntity);
                },
                indexBarItemHeight: 25,
                indexBarData: _indexBarData
              );
            }
          )
        ),
      )
    );
  }

  Widget _buildHeader(BankProvider provider) {
    return SizedBox(
      height: provider.commonUseBankList!.length*40+80,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          Padding(
            padding: const EdgeInsets.only(top: 10.0, bottom: 10.0, left: 16.0,right: 16.0),
            child: Text('${DeerLocalizations.of(context)!.commonlyUsed.nullSafe}', style: Theme.of(context).textTheme.titleSmall),
          ),
          Expanded(
            child: ListView.builder(
              physics: const NeverScrollableScrollPhysics(),
              itemExtent: 40.0,
              itemCount: provider?.commonUseBankList?.length??0,
              itemBuilder: (_, int index) {
                BankEntity bankEntity = provider?.commonUseBankList?.elementAt(index)??BankEntity();
                return InkWell(
                  onTap: () => NavigatorUtils.goBackWithParams(context, bankEntity),
                  child: Padding(
                    padding: const EdgeInsets.only(left: 0.0,right: 0.0,bottom: 5.0,top: 5.0),
                    child: ListTile(leading: Image(image: ImageUtils.getImageProvider(Constant.imageServerHost+'${bankEntity.cover}', holderImg: 'shop/tx'),height: 50,fit: BoxFit.fitHeight,),trailing: Text(bankEntity.name??''),),
                  ),
                );
              }
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(top: 20.0, bottom: 0.0, left: 16.0),
            child: Text('${DeerLocalizations.of(context)!.allBank.nullSafe}', style: Theme.of(context).textTheme.titleSmall),
          ),
        ],
      ),
    );
  }

  Widget _buildListItem(BankEntity bankEntity) {
    return InkWell(
      onTap: () => NavigatorUtils.goBackWithParams(context, bankEntity),
      child: Container(
        padding: const EdgeInsets.only(left: 16.0, right: 34.0),
        height: 40.0,
        child: Container(
          decoration: BoxDecoration(
            border: (bankEntity.isShowSuspension && bankEntity.id != 17749) ? Border(
              top: Divider.createBorderSide(context, width: 0.6),
            ) : null
          ),
          child: Row(
            children: <Widget>[
              Opacity(
                opacity: bankEntity.isShowSuspension ? 1 : 0,
                child: SizedBox(
                  width: 28.0,
                  child: Text(bankEntity.letter.nullSafe),
                )
              ),
              Expanded(
                child: Text(bankEntity.name.nullSafe),
              )
            ],
          ),
        ),
      ),
    );
  }
}
